package org.xx.armory.spring5.wxwork;

import org.apache.commons.lang3.ArrayUtils;

import static org.apache.commons.lang3.StringUtils.stripToEmpty;
import static org.xx.armory.commons.ArrayUtils.disjoint;

/**
 * 企业微信标签。
 *
 * 可以为用户或者部门指定标签。
 */
public final class WxWorkTag {
    private final int id;
    private final String title;
    private final String[] userIds;
    private final String[] departmentIds;

    public WxWorkTag(
            int id,
            String title,
            String[] userIds,
            String[] departmentIds
    ) {
        this.id = id;
        this.title = stripToEmpty(title);
        this.userIds = ArrayUtils.clone(userIds);
        this.departmentIds = ArrayUtils.clone(departmentIds);
    }

    public int getId() {
        return this.id;
    }

    public String getTitle() {
        return this.title;
    }

    public String[] getUserIds() {
        return ArrayUtils.clone(this.userIds);
    }

    public String[] getDepartmentIds() {
        return ArrayUtils.clone(this.departmentIds);
    }

    public boolean containsUserId(
            String userId
    ) {
        return ArrayUtils.contains(this.userIds, userId);
    }

    public boolean containsDepartmentId(
            String... departmentIds
    ) {
        return !disjoint(this.departmentIds, departmentIds);
    }

    @Override
    public int hashCode() {
        return this.id;
    }

    @Override
    public boolean equals(
            Object obj
    ) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof WxWorkTag)) {
            return false;
        }
        final var other = (WxWorkTag) obj;
        return this.id == other.id;
    }

    @Override
    public String toString() {
        return "WxWorkTag(id=" + this.id + ",title=" + this.title + ")@" + Integer.toHexString(System.identityHashCode(this));
    }
}
